/*
 *  $Id: $
 *
 *  Part of INX: INterfaces in Xml.
 *  Copyright (C) 2007 David Griffiths
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2.1 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */

package dg.inx;
import java.awt.Frame;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.ActionMap;
import javax.swing.InputMap;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JPanel;
import javax.swing.KeyStroke;

/**
 *
 * @author  davidg
 */
public final class OKCancelEditor extends Editor {
    private boolean isCancelled = true;

    public OKCancelEditor(final Frame parent, final String title,
            final AbstractModel model, final String viewXML) {
        super(parent, title, model, viewXML);
    }

    protected void addOther(final GridBagLayout gbl, final JPanel pan2, final GridBagConstraints c) {
        JPanel panButtons = this.createOKCancel();
        c.insets = new Insets(0, 20, 20, 20);
        c.weighty = 0.0;
        c.fill = GridBagConstraints.BOTH;
        c.gridheight = GridBagConstraints.REMAINDER;
        gbl.setConstraints(panButtons, c);
        pan2.add(panButtons);
    }

    private JPanel createOKCancel() {
        JPanel panel = new JPanel();
        GridBagLayout gbl = new GridBagLayout();
        panel.setLayout(gbl);
        GridBagConstraints con = new GridBagConstraints();
        con.gridwidth = GridBagConstraints.RELATIVE;
        con.anchor = GridBagConstraints.EAST;
        con.insets = new Insets(0, 0, 0, 0);
        con.weightx = 10.0;

        JButton btnCancel = new JButton("Cancel");
        gbl.setConstraints(btnCancel, con);
        panel.add(btnCancel);
        btnCancel.addActionListener(new ActionListener() {
            public void actionPerformed(final ActionEvent evt) {
                cancel();
            }
        });

        con.gridwidth = GridBagConstraints.REMAINDER;
        con.weightx = 0.0;

        JButton btnOK = new JButton("OK");
        this.getRootPane().setDefaultButton(btnOK);
        gbl.setConstraints(btnOK, con);
        panel.add(btnOK);
        btnOK.addActionListener(new ActionListener() {
            public void actionPerformed(final ActionEvent evt) {
                ok();
            }
        });
        // Bind ESCAPE to cancel
        bindKeyToAction(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0),
                new AbstractAction() {
            public void actionPerformed(ActionEvent ae) {
                cancel();
            }
        });
        return panel;
    }

    private void ok() {
        this.isCancelled = false;
        this.setVisible(false);
    }

    private void cancel() {
        this.isCancelled = true;
        this.setVisible(false);
    }

    public boolean isCancelled() {
        return this.isCancelled;
    }

    /**
     * Bind a key-press to the given action.
     *@param keyStroke - key-stroke object.
     *@param action - action to execute when the key's pressed.
     */
    protected void bindKeyToAction(final KeyStroke keyStroke,
            final Action action) {
        // Bind the Escape key to the "cancel" command
        InputMap map = this.getRootPane().getInputMap(
                JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT );
        map.put(keyStroke, action.toString());
        // Bind the "cancel" command to the cancel() method
        ActionMap actionMap = this.getRootPane().getActionMap();
        actionMap.put(action.toString(), action);
    }
}
